<?php

namespace app\index\home;
use think\Controller;
use think\Db;

class User extends Controller
{
    protected function initialize() {
        header("Access-Control-Allow-Origin: *");//允许所有地址跨域请求
    }
	/**
	 * [doLogin 用户登陆]
	 * @return [type] [description]
	 */
    public function doLogin()
    {
        $identifier = input('identifier');
        $password = input('password');

        if(!$identifier || !$password){
          $data['code'] = false;
            $data['msg']  = '请填写账号或密码';
            return json($data);
        }
        $user = Db::name('user_auths')->where('identity_type','username')->where("identifier",$identifier)->find();

        if(!$user){
            $data['code'] = false;
            $data['msg']  = '账号不存在';
            return json($data);
        }else if(!password_verify($password,$user['credential'])){
            $data['code'] = false;
            $data['msg']  = '账号或密码错误';
            return json($data);
        }else{
            $groupInfo = Db::name('user_auths')->where('id',$user['id'])->setInc('login_num');
          	$userInfo = Db::name('users')->where('id',$user['user_id'])->find();
            $data['code'] = true;
            $data['msg']  = '登录成功';

          	$array = [
                'token'=>getToken($user['user_id']),
            	'user_name'=>$userInfo['nickname'],
                'user_avatar'=>'http://pic.qinty.net/upload/sys/image/57/f68782e4e82f2feb43da504f0e819d.jpg',
            ];
            $data['data'] = $array;
            return json($data);
        }
    }
}
